package com.code.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	
	public static String format() {
		return format(new Date(), "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String format(String pattern) {
		return format(new Date(), pattern);
	}
	
	public static String format(Date date) {
		return format("yyyy-MM-dd HH:mm:ss");
	}
	
	public static String format(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	public static Date parse(String str) {
		return parse(str, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static Date parse(String str, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static Timestamp parseTimestamp(String str) {
		return parseTimestamp(parse(str));
	}
	
	public static Timestamp parseTimestamp(String str, String pattern) {
		return parseTimestamp(parse(str, pattern));
	}
	
	
	public static Timestamp parseTimestamp(Date date) {
		return new Timestamp(date.getTime());
	}
	
	public static void main(String[] args) {
		Timestamp s = parseTimestamp("2012-12-12 10:12:34");
		System.out.println(s);
		Date d = parse("2012-12-12 10:12:34");
		System.out.println(d);
	}
	
}
